{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e8710d6f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4d41ffe2",
   "metadata": {},
   "outputs": [],
   "source": [
    "class AudioCaptureDevice():\n",
    "    def __init__(self,\n",
    "                 device_name: str,\n",
    "                 output_format: str,\n",
    "                 channels: int,\n",
    "                 sample_rate: int,\n",
    "                 buffer_duration: float\n",
    "                 ):\n",
    "        self.commands = [\n",
    "            'arecord',\n",
    "            '-D', device_name,\n",
    "            '-f', output_format,\n",
    "            '-c', str(channels),\n",
    "            '-r', str(sample_rate),\n",
    "            '-t', 'raw'\n",
    "            ]\n",
    "        self.chunk_size = int(sample_rate/10*2*channels)\n",
    "        self.buffer_size = int(buffer_duration * sample_rate / (self.chunk_size // channels))\n",
    "        self.is_running = False\n",
    "        self.read_write_lock = threading.Lock()\n",
    "        self.audio_buffer = deque(maxlen=self.buffer_size)\n",
    "    def capture_process(self):\n",
    "        self.process = subprocess.Popen(self.commands,stdout=subprocess.PIPE,stderr=subprocess.PIPE)\n",
    "        while self.is_running:\n",
    "            chunk = self.process.stdout.read()\n",
    "            if not chunk:\n",
    "                break\n",
    "            with self.read_write_lock:\n",
    "                self.audio_buffer.append()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pytorch",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
